package com.returnsoft.softphone.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.naming.InitialContext;

import com.returnsoft.security.exception.SecurityExcepcion;
import com.returnsoft.softphone.dto.CampaignDto;
import com.returnsoft.softphone.dto.SessionDto;
import com.returnsoft.softphone.dto.SessionTypeDto;
import com.returnsoft.softphone.dto.SupervisorDto;
import com.returnsoft.softphone.exception.CCenterExcepcion;
import com.returnsoft.softphone.service.SessionInterface;
import com.returnsoft.softphone.service.SessionTypeInterface;
import com.returnsoft.softphone.service.SupervisorInterface;
import com.returnsoft.util.Util;

@ManagedBean
@ViewScoped
public class SessionController implements Serializable {

	private static final long serialVersionUID = 1L;

	private List<SessionDto> sessions;
	
	private List<SelectItem> campaigns;
	private List<SelectItem> sessionTypes;
	private List<SelectItem> supervisors;
	
	private String campaignSelected;
	private String sessionTypeSelected;
	private String supervisorSelected;
	private Boolean openSessionsSelected;
	
	private Date startedAt;
	private String agentCode;
	private String agentDocumentNumber;
	private String agentName;
	private String agentUsername;
	private String sessionHost;
	private String sessionPeer;
	

	public void beforeSearchSupervisor() {
		
		try {
			if (sessionTypes == null || sessionTypes.size() == 0) {
				SessionBean session = (SessionBean) FacesContext
						.getCurrentInstance().getExternalContext()
						.getSessionMap().get("sessionBean");
				Integer supervisorId = session.getSupervisorId();

				SessionTypeInterface sessionTypeService = (SessionTypeInterface) new InitialContext(
						Util.getInitProperties())
						.lookup("java:global/CCenterEAR/CCenterEJB/SessionTypeBean"
								+ "!com.returnsoft.softphone.service.SessionTypeInterface");
				List<SessionTypeDto> sessionTypesDto = sessionTypeService
						.findTypesBySupervisor(supervisorId);

				sessionTypes = new ArrayList<SelectItem>();
				for (SessionTypeDto sessionTypeDto : sessionTypesDto) {
					SelectItem item = new SelectItem();
					item.setValue(sessionTypeDto.getId().toString());
					item.setLabel(sessionTypeDto.getName());
					sessionTypes.add(item);
				}
			}

			if (campaigns == null || campaigns.size() == 0) {
				SessionBean session = (SessionBean) FacesContext
						.getCurrentInstance().getExternalContext()
						.getSessionMap().get("sessionBean");
				Integer supervisorId = session.getSupervisorId();

				SupervisorInterface supervisorService = (SupervisorInterface) new InitialContext(
						Util.getInitProperties())
						.lookup("java:global/CCenterEAR/CCenterEJB/SupervisorBean"
								+ "!com.returnsoft.softphone.service.SupervisorInterface");
				List<CampaignDto> campaignsDto = supervisorService
						.findCampaigns(supervisorId);

				campaigns = new ArrayList<SelectItem>();
				for (CampaignDto campaignDto : campaignsDto) {
					SelectItem item = new SelectItem();
					item.setValue(campaignDto.getId().toString());
					item.setLabel(campaignDto.getName());
					campaigns.add(item);
				}
			}

		} catch (Exception e) {
			if (!(e instanceof CCenterExcepcion)
					&& !(e instanceof SecurityExcepcion))
				e.printStackTrace();
			FacesMessage msg = new FacesMessage(e.getMessage(), e.getMessage());
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
	}
	
	public void searchSupervisors() {

		System.out.println("Ingreso a search supervisors");
		try {
			System.out.println("" + campaignSelected);
			
			if (campaignSelected != null && !campaignSelected.equals("")) {
				SupervisorInterface supervisorService = (SupervisorInterface) new InitialContext(
						Util.getInitProperties())
						.lookup("java:global/CCenterEAR/CCenterEJB/SupervisorBean"
								+ "!com.returnsoft.softphone.service.SupervisorInterface");
				supervisors = new ArrayList<SelectItem>();
				List<Integer> campaignsId = new ArrayList<Integer>();

				if (campaignSelected.equals("t")) {
					if (campaigns != null && campaigns.size() > 0) {
						for (SelectItem item : campaigns) {
							campaignsId.add(Integer.parseInt(item.getValue()
									.toString()));
						}
					}
				} else if(campaignSelected.equals("s")) {
					supervisors = new ArrayList<SelectItem>();
				} else {
					campaignsId.add(Integer.parseInt(campaignSelected));
				}

				List<SupervisorDto> supervisorsDto = supervisorService
						.find(campaignsId);
				for (SupervisorDto supervisorDto : supervisorsDto) {
					SelectItem item = new SelectItem();
					item.setValue(supervisorDto.getId());
					item.setLabel(supervisorDto.getFirstname()+" "+supervisorDto.getLastname());
					supervisors.add(item);
				}

			} else {
				supervisors = new ArrayList<SelectItem>();
			}
		} catch (Exception e) {
			if (!(e instanceof CCenterExcepcion)
					&& !(e instanceof SecurityExcepcion))
				e.printStackTrace();
			FacesMessage msg = new FacesMessage(
					e.getMessage(), e.getMessage());
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
	}

	

	public String searchAgentSessions() {

		try {
			SessionBean session = (SessionBean) FacesContext
					.getCurrentInstance().getExternalContext().getSessionMap()
					.get("sessionBean");
			Integer agentId = session.getAgentId();

			SessionInterface sessionService = (SessionInterface) new InitialContext(
					Util.getInitProperties())
					.lookup("java:global/CCenterEAR/CCenterEJB/SessionBean"
							+ "!com.returnsoft.softphone.service.SessionInterface");

			sessions = sessionService.findSessionsForAgent(startedAt, agentId);

			return null;

		} catch (Exception e) {
			if (!(e instanceof CCenterExcepcion)
					&& !(e instanceof SecurityExcepcion))
				e.printStackTrace();
			FacesMessage msg = new FacesMessage(e.getMessage(), e.getMessage());
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return null;
		}
	}

	public String searchSupervisorSessions() {

		try {

			SessionInterface sessionService = (SessionInterface) new InitialContext(
					Util.getInitProperties())
					.lookup("java:global/CCenterEAR/CCenterEJB/SessionBean"
							+ "!com.returnsoft.softphone.service.SessionInterface");

			List<Integer> sessionTypeIds = new ArrayList<Integer>();
			if (sessionTypeSelected != null && !sessionTypeSelected.equals("")) {
				if (sessionTypeSelected.equals("t")) {
					if (sessionTypes != null && sessionTypes.size() > 0) {
						for (SelectItem item : sessionTypes) {

							sessionTypeIds.add(Integer.parseInt(item.getValue()
									.toString()));
						}
					}
				} else {
					sessionTypeIds.add(Integer.parseInt(sessionTypeSelected));
				}
			}

			List<Integer> campaignIds = new ArrayList<Integer>();
			if (campaignSelected != null && !campaignSelected.equals("")) {
				if (campaignSelected.equals("t")) {
					if (campaigns != null && campaigns.size() > 0) {
						for (SelectItem item : campaigns) {
							campaignIds.add(Integer.parseInt(item.getValue()
									.toString()));
						}
					}
				} else {
					campaignIds.add(Integer.parseInt(campaignSelected));
				}
			}
			
			List<Integer> supervisorIds = new ArrayList<Integer>();
			if (supervisorSelected != null && !supervisorSelected.equals("")) {
				if (supervisorSelected.equals("t")) {
					if (supervisors != null && supervisors.size() > 0) {
						for (SelectItem item : supervisors) {
							supervisorIds.add(Integer.parseInt(item.getValue()
									.toString()));
						}
					}
				} else {
					supervisorIds.add(Integer.parseInt(supervisorSelected));
				}
			}
			
			sessions = sessionService.findSessionsForSupervisor(sessionTypeIds,campaignIds,
					supervisorIds,startedAt,agentCode,agentName,agentDocumentNumber,agentUsername,sessionHost,
					sessionPeer,openSessionsSelected);

			return null;

		} catch (Exception e) {
			if (!(e instanceof CCenterExcepcion)
					&& !(e instanceof SecurityExcepcion))
				e.printStackTrace();
			FacesMessage msg = new FacesMessage(e.getMessage(), e.getMessage());
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return null;
		}
	}

	public List<SelectItem> getSessionTypes() {
		return sessionTypes;
	}

	public void setSessionTypes(List<SelectItem> sessionTypes) {
		this.sessionTypes = sessionTypes;
	}

	public List<SessionDto> getSessions() {
		return sessions;
	}

	public void setSessions(List<SessionDto> sessions) {
		this.sessions = sessions;
	}

	public Date getStartedAt() {
		return startedAt;
	}

	public void setStartedAt(Date startedAt) {
		this.startedAt = startedAt;
	}

	

	public String getCampaignSelected() {
		return campaignSelected;
	}

	public void setCampaignSelected(String campaignSelected) {
		this.campaignSelected = campaignSelected;
	}

	public String getSessionTypeSelected() {
		return sessionTypeSelected;
	}

	public void setSessionTypeSelected(String sessionTypeSelected) {
		this.sessionTypeSelected = sessionTypeSelected;
	}

	public String getAgentCode() {
		return agentCode;
	}

	public void setAgentCode(String agentCode) {
		this.agentCode = agentCode;
	}

	public List<SelectItem> getCampaigns() {
		return campaigns;
	}

	public void setCampaigns(List<SelectItem> campaigns) {
		this.campaigns = campaigns;
	}

	public List<SelectItem> getSupervisors() {
		return supervisors;
	}

	public void setSupervisors(List<SelectItem> supervisors) {
		this.supervisors = supervisors;
	}

	public String getSupervisorSelected() {
		return supervisorSelected;
	}

	public void setSupervisorSelected(String supervisorSelected) {
		this.supervisorSelected = supervisorSelected;
	}

	public Boolean getOpenSessionsSelected() {
		return openSessionsSelected;
	}

	public void setOpenSessionsSelected(Boolean openSessionsSelected) {
		this.openSessionsSelected = openSessionsSelected;
	}

	public String getAgentDocumentNumber() {
		return agentDocumentNumber;
	}

	public void setAgentDocumentNumber(String agentDocumentNumber) {
		this.agentDocumentNumber = agentDocumentNumber;
	}

	public String getAgentName() {
		return agentName;
	}

	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}

	public String getAgentUsername() {
		return agentUsername;
	}

	public void setAgentUsername(String agentUsername) {
		this.agentUsername = agentUsername;
	}

	public String getSessionHost() {
		return sessionHost;
	}

	public void setSessionHost(String sessionHost) {
		this.sessionHost = sessionHost;
	}

	public String getSessionPeer() {
		return sessionPeer;
	}

	public void setSessionPeer(String sessionPeer) {
		this.sessionPeer = sessionPeer;
	}

	

}
